//
//  KDReginPickerController.m
//  WangJingApp
//
//  Created by 魏强 on 17/10/13.
//  Copyright © 2017年 KDTech. All rights reserved.
//

#import "KDReginPickerController.h"
#import "KDCity.h"
#import "UIViewController+BackButtonHandler.h"

@interface KDReginPickerController ()<UITableViewDelegate,UITableViewDataSource,BackButtonHandlerProtocol>
@property (nonatomic, weak) UITableView *tableView;
@end
@implementation KDReginPickerController
#pragma mark - 懒加载区

//- (NSArray *)sources
//{
//    if (!_sources) {
//        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Source" ofType:@"plist"];
//
//            _sources =  [KDCity objectArrayWithFile:plistPath];
//
////        _sources = ;
//    }
//    return _sources;
//}
#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackGroundColor;
    // Do any additional setup after loading the view.
    [self setupTableView];
}

#pragma mark - 板块布置区
- (void)setupTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0  , 0, screenW,  screenH - 64) style:UITableViewStyleGrouped];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tag = 1000;
    tableView.tableFooterView = [UIView new];
    tableView.separatorColor = BackGroundColor;
    tableView.backgroundColor = BackGroundColor;
    tableView.allowsMultipleSelection = YES;
    tableView.allowsMultipleSelectionDuringEditing = YES;
    [self.view addSubview:tableView];
}
#pragma mark - 事件点击区

#pragma mark - 网络请求区

#pragma mark - 通知接收区




#pragma mark -UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.citys.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId =@"cellName";
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.selectedBackgroundView = [[UIView alloc]init];
    }
    KDCity *source = self.citys[indexPath.row];
    cell.textLabel.text = source.city_name;
    cell.accessoryType = (source.check)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
    //    thisCell.accessoryView.hidden = NO;
    KDCity *source = self.citys[indexPath.row];
    source.check = !source.check;
    thisCell.accessoryType = (source.check)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
    
    KDCity *source = self.citys[indexPath.row];
    source.check = !source.check;
    thisCell.accessoryType = (source.check)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
    
}
#pragma mark - BackButtonHandlerProtocol
- (BOOL)navigationShouldPopOnBackButton{
    if (_delegate &&[_delegate respondsToSelector:@selector(reginPickerControllerPopWithCitys:)]) {
        [_delegate reginPickerControllerPopWithCitys:self.citys];
    }
    
    return YES;
}


@end
